
package code.codeworld.space.blog.controller;


import code.codeworld.space.blog.model.Blog;
import code.codeworld.space.blog.model.vo.BlogVo;
import code.codeworld.space.blog.service.BlogService;
import code.codeworld.common.kits.Request;
import code.codeworld.common.kits.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * world控制器
 *
 * @author freecode
 * @date 2023/03/23 00:20
 */
@CrossOrigin
@RequestMapping(Request.CODEWORLD_WEB)
@RestController
@Validated
public class BlogCtl {
    @Resource
    private BlogService blogService;


    /**
     * 添加blog
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @PostMapping("blog/add")
    public Result<String> add(@RequestBody Blog blog) {
        //废弃
        if (!ObjectUtils.isEmpty(blog)) {
            return Result.absent();
        }
        Date now = new Date();
        blog.setCreateTime(now);
        blog.setUpdateTime(now);
        blog.setStarNum(BigDecimal.ROUND_DOWN);
        blog.setDeleteFlag(BigDecimal.ROUND_UP);
        return blogService.add(blog);
    }

    /**
     * 获取blog分页
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @GetMapping("blog/page")
    public Page<Blog> page(BlogVo blogVo) {
        return blogService.page(blogVo);
    }

    /**
     * 获取blog分页
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @GetMapping("blog/myPage")
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<Blog> myPage(BlogVo blogVo) {
        return blogService.myPage(blogVo);
    }

    /**
     * 获取blog详情
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @GetMapping("blog/detail")
    public Result<Blog> detail(@Valid Blog blog) {
        return Result.data(blogService.detail(blog));
    }

    /**
     * 编辑blog
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @PostMapping("blog/edit")
    public Result<String> edit(@RequestBody @Valid Blog blog) {
        //废弃
        if (!ObjectUtils.isEmpty(blog)) {
            return Result.absent();
        }
        return blogService.edit(blog);
    }

    /**
     * 编辑blog标注:星星、
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("blog/editMark")
    public Result<String> editMark(@RequestBody @Valid BlogVo blogVo) {
        if (blogVo.getId() == null) {
            return Result.paramError();
        }
        if (blogVo.getStarNum() == null && blogVo.getCollectNum() == null && StringUtils.isEmpty(blogVo.getComment())) {
            return Result.paramError();
        }
        return blogService.editMark(blogVo);
    }

    /**
     * 删除blog 隐藏/清除
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("blog/delete")
    public Result<String> delete(@RequestBody @Valid Blog blog) {
        //废弃
        if (!ObjectUtils.isEmpty(blog)) {
            return Result.absent();
        }
        return blogService.delete(blog);
    }

    /**
     * 批量删除blog 隐藏/清除
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("blog/deleteList")
    public Result<String> deleteList(@RequestBody @Valid List<Blog> blogList) {
        //废弃
        if (!ObjectUtils.isEmpty(blogList)) {
            return Result.absent();
        }
        return blogService.deleteList(blogList);
    }

}
